package com.sm.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.sm.model.Collect;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sm.model.CourseCollect;
import com.sm.model.GoodsBean;
import com.sm.service.CollectService;
import com.sm.util.Msg;

@Controller
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 添加收藏
     *
     * @param userId
     * @param collectType
     * @param courseGoodsId
     * @return
     */
    @RequestMapping("/addCollect")
    @ResponseBody
    public Msg addCollect(Integer userId, Integer collectType, Integer courseGoodsId) {
        if (userId == null || collectType == null || courseGoodsId == null) {
            return Msg.fail().add("msg", "参数不为空！");
        }
        Collect collect = collectService.selectCollectBy(userId, collectType, courseGoodsId);
        if (collect != null) {
            return Msg.fail().add("msg", "不可重复收藏！");
        } else {
            // 创建对象
            Collect collectn = new Collect();
            collectn.setUserId(userId);
            collectn.setCollectType(collectType);
            collectn.setCourseGoodsId(courseGoodsId);
            // 添加收藏
            int val = collectService.insertSelective(collectn);
            if (val == 1) {
                return Msg.success().add("msg", "添加成功");
            } else {
                return Msg.fail().add("msg", "添加失败");
            }
        }


    }

    /**
     * 查询收藏的课程，商品
     *
     * @param userId
     * @param
     * @return
     */
    @RequestMapping("/selectCollect")
    @ResponseBody
    public Msg selectCollect(Integer userId) {
        if (userId == null) {
            return Msg.fail().add("msg", "参数不为空！");
        }

        // 查询课程
        List<CourseCollect> clist = collectService.selectCourseCollect(userId);

        // 查询超市商品的收藏
        List<GoodsBean> glist = collectService.selectGoodsCollect(userId);
        return Msg.success().add("clist", clist).add("cglist", glist);

    }

    /**
     * 批量删除收藏
     *
     * @param
     * @return
     */
    @RequestMapping("/deleteCollect")
    @Transactional
    @ResponseBody
    public Msg deleteCollect(@RequestParam("collectIds") List<Integer> collectIds) {
        if (collectIds == null) {
            return Msg.fail().add("msg", "参数不为空！");
        }
        collectService.deleteCollect(collectIds.toArray(new Integer[0]));
        return Msg.success().add("msg", "删除成功");

    }


    /**
     * 取消收藏（课程，商品，幸福问答等通用）
     */
    @RequestMapping("/cancelCollect")
    @ResponseBody
    public Msg cancelCollect(Integer userId, Integer collectType, Integer courseGoodsId){
        if (userId == null||collectType == null||courseGoodsId == null){
            return Msg.fail().add("msg","参数异常");
        }
        int i = collectService.cancelCollect(userId, collectType, courseGoodsId);
        if (i == 1){
            return Msg.success();
        }
        return Msg.fail();
    }





}
